package com.easyapi.web.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyapi.common.base.UserTokenThreadHolder;
import com.easyapi.common.contants.Constants;
import com.easyapi.common.enums.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.common.param.user.*;
import com.easyapi.common.utils.BaseResponse;
import com.easyapi.common.utils.ResultUtils;
import com.easyapi.common.vo.user.ApiCenterUserVo;
import com.easyapi.common.vo.user.RoleVo;
import com.easyapi.server.service.captcha.CaptchaService;
import com.easyapi.server.service.user.RoleService;
import com.easyapi.server.service.user.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @author javadadi
 */
@RestController
@RequestMapping(Constants.BASE_URL + "user")
@Api(tags = {"EUserCenter"})
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private CaptchaService captchaService;
    @Resource
    private RoleService roleService;

    @GetMapping("/getCode")
    public BaseResponse<Captcha> getCode() {
        return captchaService.getCode(null);
    }

    @PostMapping("/reGetCode")
    public BaseResponse<Captcha> reGetCode(@RequestBody Captcha captcha) {
        if (Objects.isNull(captcha)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (StringUtils.isEmpty(captcha.getUuid())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        return captchaService.getCode(captcha.getUuid());
    }

    @PostMapping("/login")
    public BaseResponse<Boolean> login(@RequestBody UserLoginParam param, HttpServletResponse response, HttpServletRequest request) {
        if (Objects.isNull(param)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (StringUtils.isAnyBlank(param.getUserAccount(), param.getUserPassword())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (StringUtils.isAnyBlank(param.getUuid(), param.getCode())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        userService.login(response, request, param.getUserAccount(), param.getUserPassword(), param.getUuid(), param.getCode());
        return ResultUtils.success(true);
    }

    @PostMapping("/register")
    public BaseResponse<Boolean> register(@RequestBody UserRegisterParam param) {
        if (Objects.isNull(param)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (StringUtils.isAnyBlank(param.getUserAccount(), param.getUserPassword(), param.getUserPasswordCheck())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        userService.register(param.getUserAccount(), param.getUserPassword(), param.getUserPasswordCheck());
        return ResultUtils.success(true);
    }

    @PostMapping("/empowerOrRevoke")
    public BaseResponse<Boolean> empowerOrRevoke(@RequestBody UserOperationParam param) {
        if (Objects.isNull(param) || Objects.isNull(param.getUserId())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (StringUtils.isAnyBlank(param.getAction())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        userService.userOperation(param.getUserId(), param.getAction(), param.getRoleId());
        return ResultUtils.success(true);
    }

    @PostMapping("/bindOrLife")
    public BaseResponse<Boolean> bindOrLife(@RequestBody UserOperationParam param) {
        if (Objects.isNull(param) || Objects.isNull(param.getUserId())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (StringUtils.isEmpty(param.getAction())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        userService.userOperation(param.getUserId(), param.getAction(), param.getRoleId());
        return ResultUtils.success(true);
    }

    @PostMapping("/loginOut")
    public BaseResponse<Boolean> loginOut(HttpServletResponse response) {
        userService.loginOut(response);
        return ResultUtils.success(true);
    }

    @PostMapping("/pageUsers")
    public BaseResponse<Page<ApiCenterUserVo>> pageUsers(@RequestBody UserPageParam param) {
        if (Objects.isNull(param)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        Page<ApiCenterUserVo> page = userService.page(param);
        return ResultUtils.success(page);
    }

    @PostMapping("/currentUser")
    public BaseResponse<ApiCenterUserVo> currentUser() {
        ApiCenterUserVo currentUser = UserTokenThreadHolder.getCurrentUser();
        return ResultUtils.success(currentUser);
    }

    @PostMapping("/listRoleVO")
    public BaseResponse<List<RoleVo>> listUserExRoles(@RequestBody UserOperationParam param) {
        if(Objects.nonNull(param.getUserId()) && param.getUserId()<=0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<RoleVo> roleVos = roleService.listUserExRoles(param.getUserId());
        return ResultUtils.success(roleVos);
    }

    @PostMapping("/getRolesByUserId")
    public BaseResponse<List<RoleVo>> listRoleVosByUserId(@RequestBody UserOperationParam param) {
        if (Objects.isNull(param) || Objects.isNull(param.getUserId())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<RoleVo> roleVos = roleService.listRoleVosByUserId(param.getUserId());
        return ResultUtils.success(roleVos);
    }

    @PostMapping("/getUserVoById")
    public BaseResponse<ApiCenterUserVo> getUserVoById(@RequestBody UserOperationParam param) {
        if (Objects.isNull(param.getUserId()) || param.getUserId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterUserVo userById = userService.getUserById(param.getUserId());
        return ResultUtils.success(userById);
    }

}
